﻿// CsHTTPServer
//
// rmortega77@yahoo.es
// The use of this software is subject to the following agreement
//
// 1. Don't use it to kill.
// 2. Don't use to lie.
// 3. If you learned something give it back.
// 4. If you make money with it, consider sharing with the author.
// 5. If you do not complies with 1 to 5, you may not use this software.
//
// If you have money to spare, and found useful, or funny, or anything 
// worth on this software, and want to contribute with future free 
// software development.
// You may contact the author at rmortega77@yahoo.es 
// Contributions can be from money to hardware spareparts (better), or 
// a bug fix (best), or printed bibliografy, or thanks... 
// just write me.

using System;
using System.Text;

using System.IO;
using System.Web;

using Microsoft.Win32;

namespace Tornado.lib.CsHTTPServer
{
    /// <summary>
    /// Summary description for MyServer.
    /// </summary>
    public class MyServer : CsHTTPServer
    {
        public string Folder;

        public MyServer()
            : base()
        {
            this.Folder = "c:\\www";
        }

        public MyServer(int thePort, string theFolder)
            : base(thePort)
        {
            this.Folder = theFolder;
        }

        public override void OnResponse(ref HTTPRequestStruct rq, ref HTTPResponseStruct rp)
        {
            string path = this.Folder + "\\" + rq.URL.Replace("/", "\\");

            if (Directory.Exists(path))
            {
                if (File.Exists(path + "default.htm"))
                    path += "\\default.htm";
                else
                {
                    string[] dirs = Directory.GetDirectories(path);
                    string[] files = Directory.GetFiles(path);

                    string bodyStr = "<!DOCTYPE html> <html> <head> <meta charset=\"utf-8\" /> <title>File Server | Tornado v1.0.0 </title> <style type=\"text/css\"> *{ margin:0; padding:0; } html{ background-color: #858285; background-image: -moz-radial-gradient(circle closest-corner, #858285 0%, #656565 100%); background-image: -o-radial-gradient(circle closest-corner, #858285 0%, #656565 100%); background-image: -webkit-radial-gradient(circle closest-corner, #858285 0%, #656565 100%); background-image: radial-gradient(circle closest-corner, #858285 0%, #656565 100%); } body{ font:14px/1.3 'Segoe UI',Arial, sans-serif; background-position: center center; background-repeat: no-repeat; } a, a:visited { outline:none; color:#1c4f64; } a:hover{ text-decoration:none; } section, footer, header{ display: block; } #main{ padding: 100px 0 150px 0; margin: 0 auto; width: 390px; } footer{ background-color: #151515; bottom: 0; box-shadow: 0 -1px 2px rgba(0,0,0,0.4); height: 40px; left: 0; position: fixed; width: 100%; z-index: 100000; } footer h2{ font-size: 14px; font-weight: normal; position: absolute; top:0; left: 50%; bottom:0; margin-left: -400px; padding:13px 20px 0; border:1px solid #000; border-width:0 1px; background-color:rgba(0,0,0,0.6); box-shadow:0 0 5px #000 inset; line-height: 1; margin-top: 0; } footer h2 a{ color: #EEEEEE !important; text-decoration:none !important; } footer h2 i{ font-style:normal; color:#888; } #tzine-download{ opacity:0.9; background-color:#257691; font-size:11px; text-align:center; text-transform:uppercase; width:110px; height:28px; line-height:28px; text-decoration:none !important; display: inline-block; border-radius: 2px; color: #fff !Important; font-weight: bold; box-shadow: 0 -1px 2px #1e5e74 inset; border-top:1px solid #26849c; text-shadow:1px 1px 1px #1e6176; margin-top:6px; } #tzine-download:hover{ opacity:1; } #tzine-actions{ position:absolute; top:0; width:500px; right:50%; margin-right:-400px; text-align:right; } #tzine-actions iframe{ display: inline-block; height: 21px; width: 95px; position: relative; bottom: -6px; } .tn-container{ width: 390px; overflow: hidden; box-shadow: 0 0 4px #444; border-radius: 2px; } .tn-header{ background-color:#839CBF;text-align: center; color: #fff; font-size: 16px; line-height: 42px; text-shadow: 1px 1px 0 #7086a5; box-shadow: 0 1px 3px #888; z-index: 1; position: relative; height: 44px; } .tn-data{ overflow-y:scroll; background-repeat: no-repeat; background-color: #ddd; background-image: -webkit-radial-gradient(circle closest-corner, #fafafa 30%, #c9c9c9 100%); background-image: -moz-radial-gradient(circle closest-corner, #fafafa 30%, #c9c9c9 100%); background-image: -o-radial-gradient(circle closest-corner, #fafafa 30%, #c9c9c9 100%); background-image: radial-gradient(circle closest-corner, #fafafa 30%, #c9c9c9 100%); height: 380px; padding-top: 15px; } .tn-footer{background-color:#464646; height: 10px; border-radius: 0 0 3px 3px; } .tn-data ul{ list-style-type: none; margin: 0px auto; width: 290px; } .tn-post{ position:relative; border-bottom: 1px solid #dcdcdc; height: 35px; width: 290px; } .tn-post span{ font-style: italic; font-size: 11px; color: #a9a9a9; display: block; top:40px; left:45px; position:absolute; } .tn-post a{ top:10px; left:25px; width:250px; position:absolute; text-decoration: none; font-size:14px; color: #595959; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } .tn-post:before{ content:''; position: absolute; top: 25px; left: 10px; width: 24px; height: 24px; } .tn-data .no-border{ border-bottom: none !important; } </style> </head> <body> <div id=\"main\"> <div class=\"tn-container\"> <h2 class=\"tn-header\">Download presentation file</h2> <div class=\"tn-data\">";
                    bodyStr += "<ul>";
                    bodyStr += "<li class=\"tn-post\"><a href = \"..\">[ ... ]</a></li>\n";
                    for (int i = 0; i < dirs.Length; i++)
                        bodyStr += "<li class=\"tn-post\"><a href = \"" + rq.URL + Path.GetFileName(dirs[i]) + "/\">[ " + Path.GetFileName(dirs[i]) + " ]</a></li>\n";
                    bodyStr += "</ul>";
                    bodyStr += "<ul>";
                    for (int i = 0; i < files.Length; i++)
                        bodyStr += "<li class=\"tn-post\"><a href = \"" + rq.URL + Path.GetFileName(files[i]) + "\">" + Path.GetFileName(files[i]) + "</a></li>\n";
                    bodyStr += "</ul>";

                    bodyStr += "</div> <div class=\"tn-footer\"/> </div> </div> <footer> <h2><a href=\"http://muatocroi.com\"><i>Presentation helper:</i> Tornado v1.0.0</a></h2> <div id=\"tzine-actions\"> <a id=\"tzine-download\" href=\"http://play.google.com/\" title=\"Download This software!\">Get software</a> </div> </footer> </body> </html>";

                    rp.BodyData = Encoding.ASCII.GetBytes(bodyStr);
                    return;
                }
            }

            if (File.Exists(path))
            {
                rp.Headers["Content-type"] = "application/octet-stream";
                rp.BodyData = System.IO.File.ReadAllBytes(path);
            }
            else
            {

                rp.status = (int)RespState.NOT_FOUND;

                string bodyStr = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
                bodyStr += "<HTML><HEAD>\n";
                bodyStr += "<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">\n";
                bodyStr += "</HEAD>\n";
                bodyStr += "<BODY>File not found!!</BODY></HTML>\n";

                rp.BodyData = Encoding.ASCII.GetBytes(bodyStr);

            }

        }
    }
}
